# -*- coding: utf-8 -*-
import os
import random
from time import *
import appium
from selenium import webdriver
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest
from HTMLTestRunner import HTMLTestRunner
from send_mail import *
# print('selenium version = ', selenium.__version__)
class CanLanZi(unittest.TestCase):
    def setUp(self):
#-----------菜篮子---------

        self.desired_caps={
                            'platformName':'Android',#手机平台的名称
                            'version':'5.1.1',#手机平台的版本
                            'deviceName':'127.0.0.1:62001',#这是测试机的型号，可以查看手机的关于本机选项获得adb devices
                            'appPackage':'com.freshO2O',#需要测试的app包名
                            'appActivity':'com.freshO2O.ui.HomeActivity', #app启动页面
                            'automationName':'UiAutomator1',
                            'noReset':'True', #不重置  下一次app登录数据，信息等等都在里面。
                            'noSign':'True'
                            }
        self.driver=appium.webdriver.Remote('http://127.0.0.1:4723/wd/hub', self.desired_caps)#4723
    def tearDown(self):
        self.driver.quit()

    def sign_in(self):

        # 等主页面activity出现
        self.driver.wait_activity("com.freshO2O.ui.HomeActivity", 20)
        self.driver.find_element_by_id("com.freshO2O:id/home_tab_personal").click()#进入个人中心
        self.driver.find_element_by_id("com.freshO2O:id/personal_register_button").click()#点击注册按钮

        index = random.sample(range(1, 9), 8)#生成1~9随机整数
        numbers= [str(i) for i in index]
        num_str = ''.join(numbers)
        self.driver.find_element_by_id("com.freshO2O:id/account").send_keys("133",num_str)#输入账号
        self.driver.find_element_by_id("com.freshO2O:id/password").send_keys("123456")#输入密码
        self.driver.find_element_by_id("com.freshO2O:id/addr").send_keys("123456")#输入地址
        self.driver.find_element_by_id("com.freshO2O:id/register").click()#点击注册按钮
        time.sleep(3)
        #注册成功跳转到首页，拿到首页肉类图标
        home_page = self.driver.find_element_by_id("com.freshO2O:id/meal")
        #判断是否注册成功跳转到首页
        if home_page:
            print("注册已成功")
        else:
            print("注册失败")
        time.sleep(3)
#discover = unittest.defaultTestLoader.discover('./', pattern='*login_test.py', top_level_dir=None)
if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(CanLanZi("sign_in"))
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    file_path = os.path.join("./" + now + "run_case.html")
    f = open(file_path, "wb")
    runner = HTMLTestRunner(stream=f,
                            title="测试报告",
                            description="以下是用例执行情况")
    runner.run(suite)
    send_mail(file_path)